package com.asiainfo.complier.action;

import com.asiainfo.complier.editor.LineEditor;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.JButton;

import static java.lang.Runtime.getRuntime;

public class ComplierAction implements ActionListener {
	private boolean isRunning;
	private Process p;
	private Color normalColor;
	private JButton runBt;

	public void addRunCallBack(JButton runBt) {
		normalColor = runBt.getBackground();
		this.runBt = runBt;
		runBt.addActionListener(this);
	}

	/**
	 * execute
	 * 
	 * @param cmd
	 */
	public static void execute(String cmd) {
		try {
			getRuntime().exec(cmd);
		} catch (IOException e) {

			e.printStackTrace();
		}

	}

	/***
	 * executeComplier
	 * 
	 * @param cmd
	 * @param editor
	 * @param complier
	 * @return
	 */
	public void run(String cmd, final LineEditor editor) {
		try {
			runBt.setEnabled(true);
			runBt.setBackground(Color.red);
			runBt.setToolTipText("terminate");
			p = getRuntime().exec(cmd);
			InputStream fStream = p.getInputStream();
			InputStream errorStream = p.getErrorStream();
			isRunning = true;
			final BufferedReader reader = new BufferedReader(
					new InputStreamReader(fStream));
			BufferedReader errorReader = new BufferedReader(
					new InputStreamReader(errorStream));

			String line = null;

			Thread t = new Thread(new Runnable() {

				public void run() {
					String cLineString = null;
					try {
						while ((cLineString = reader.readLine()) != null) {

							editor.appendText(cLineString + "\n");
						}
					} catch (IOException e) {

						e.printStackTrace();
					}

				}
			});
			t.start();

			line = null;
			while ((line = errorReader.readLine()) != null) {
				editor.appendText(line + "\n");
			}

			p.waitFor();
			isRunning = false;
			if (runBt != null) {
				runBt.setToolTipText("");
				runBt.setBackground(normalColor);
			}
			editor.appendText("\n\nApplication Exit");
			if (runBt != null) {
				runBt.setEnabled(false);
			}

		} catch (Exception e) {
			e.printStackTrace();

		}

	}

	/***
	 * executeComplier
	 * 
	 * @param cmd
	 * @param editor
	 * @param complier
	 * @return
	 */
	public String complier(String cmd, final LineEditor editor) {
		try {
			p = getRuntime().exec(cmd);

			InputStream errorStream = p.getErrorStream();
			isRunning = true;

			final BufferedReader errorReader = new BufferedReader(
					new InputStreamReader(errorStream));

			String retString = null;
			String cLineString = null;
			try {
				while ((cLineString = errorReader.readLine()) != null) {
					retString += cLineString + "\n";
					editor.appendText(cLineString + "\n");
				}
			} catch (IOException e) {

				e.printStackTrace();
			}

			editor.appendText("complier end\n");

			p.waitFor();
			isRunning = false;

			editor.appendText("Exit\n");
			return retString;

		} catch (Exception e) {
			e.printStackTrace();
			return "";

		}

	}

	public boolean isRunning() {
		return isRunning;
	}

	public void actionPerformed(ActionEvent e) {
		if (isRunning) {
			p.destroy();
			runBt.setBackground(normalColor);
		}
		runBt.removeActionListener(this);
		runBt.setToolTipText("");
		runBt.setEnabled(false);
	}

}
